package com.ccl.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.ccl.connection.ConnectionMager;
import com.ccl.dao.INewsDay;
import com.ccl.model.NewsDay;
import com.ccl.model.User;

public class NewsDayimpl extends SuperOper implements INewsDay {
	public NewsDayimpl(){
		cm = new ConnectionMager();
	}

	public int addNewss(NewsDay N) {
		sql="insert into newsday(title,content,date,dayname) values(?,?,?,?)";
		try {
			con = cm.getConnection();
			psmt = con.prepareStatement(sql);
			psmt.setString(1, N.getTitle());
			psmt.setString(2, N.getContent());
			psmt.setString(3, N.getDate());
			psmt.setString(4, N.getDayname());
			row = psmt.executeUpdate();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			cm.close();
		}
		
		return row;

	}

	@Override
	public boolean updateNewss(NewsDay n) {
		//title,content,date,dayname
		sql = "update newsday set title=?,content=? where number=?";
		
		try {
			con = cm.getConnection();
			psmt = con.prepareStatement(sql);						
			psmt.setString(1, n.getTitle());
			psmt.setString(2, n.getContent());
            psmt.setInt(3, n.getNumber());
			row = psmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			cm.close();
		}
		return row>0?true:false;
	}

	@Override
	public List queryNewss() {
		sql = "select * from newsday order by title,number Desc";
//		sql = "select top 15 * from newsday order by number desc"; 
//		sql = "select * from newsday";
		List<NewsDay> list=new ArrayList<NewsDay>();
		try {
			con = cm.getConnection();
			psmt = con.prepareStatement(sql);
			ResultSet rs = psmt.executeQuery();
			while(rs.next()){
				NewsDay user = new NewsDay();         
                user.setNumber(rs.getInt(1));
                user.setTitle(rs.getString(2));
                user.setContent(rs.getString(3));
                user.setDate(rs.getString(4));
                user.setDayname(rs.getString(5));
                list.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			cm.close();
		}
		return list;	
	}


	public int deleteNewss(int number) {
		sql = "delete from newsday where number = " + number;
		try {
			con = cm.getConnection();
			psmt = con.prepareStatement(sql);
			row = psmt.executeUpdate();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{cm.close();}
		return row ;
	}
}

